package com.jindy.module.homepage.controller;


import com.jindy.common.core.controller.BaseController;
import com.jindy.common.core.domain.AjaxResult;
import com.jindy.common.core.domain.entity.SysUser;
import com.jindy.common.core.domain.model.LoginUser;
import com.jindy.common.exception.base.BaseException;
import com.jindy.common.utils.SecurityUtils;
import com.jindy.module.customer.domain.CustomerFollow;
import com.jindy.module.customer.dto.CustomerDto;
import com.jindy.module.homepage.service.HomepageService;
import com.jindy.module.homepage.vo.HomepageRespVO;
import com.jindy.module.homepage.vo.SimpleKanbanInConditionVO;
import com.jindy.module.order.domain.Order;
import com.jindy.system.service.ISysUserService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Optional;

/**
 * 首页Controller
 *
 * @author zhaoxi
 * @date 2022-11-18
 */
@Api(tags = "业务后台 - 首页 ")
@RestController
@RequestMapping("/module/homepage")
@Validated
@Slf4j
public class HomepageController extends BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(HomepageController.class);
    @Resource
    private HomepageService homepageService;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询首页数据列表
     */
    @PreAuthorize("@ss.hasPermi('module:homepage:list')")
    @GetMapping("/resp")
    public HomepageRespVO homepage(HomepageRespVO vo, CustomerDto dto, CustomerFollow follow, Order or){

        //添加权限
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        vo.setUserId(sysUser.getUserId());
        dto.setUserId(sysUser.getUserId());
        follow.setUserId(sysUser.getUserId());
        or.setUserId(sysUser.getUserId());
        HomepageRespVO RespVO = homepageService.selectHomepageRespVo(vo,dto,follow,or);
        return RespVO;
    }

    /**
     * 查询首页数据列表(全部)
     */
    @GetMapping("/map")
    public Map<String,Object> homepage1(Order or){
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        or.setUserId(sysUser.getUserId());
        Map<String,Object> map = homepageService.selectHomepage(or);
        return map;
    }

    /**
     * 查询首页数据列表(当天)
     */
    @GetMapping("/dayMap")
    public Map<String,Object> homepageDay(Order or){
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        or.setUserId(sysUser.getUserId());
        Map<String,Object> map = homepageService.selectHomepageDay(or);
        return map;
    }

    /**
     * 查询首页数据列表(本周)
     */
    @GetMapping("/weekMap")
    public Map<String,Object> homepageWeek(Order or){
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        or.setUserId(sysUser.getUserId());
        Map<String,Object> map = homepageService.selectHomepageWeek(or);
        return map;
    }

    /**
     * 查询首页数据列表(本月)
     */
    @GetMapping("/monthMap")
    public Map<String,Object> homepageMonth(Order or){
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        or.setUserId(sysUser.getUserId());
        Map<String,Object> map = homepageService.selectHomepageMonth(or);
        return map;
    }

    /**
     * 查询首页数据列表(本年)
     */
    @GetMapping("/yearMap")
    public Map<String,Object> homepageYear(Order or){
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        or.setUserId(sysUser.getUserId());
        Map<String,Object> map = homepageService.selectHomepageYear(or);
        return map;
    }

    /**
     * 获取各个模块的待办数据
     * @return
     */
    @GetMapping("/message/count")
    public AjaxResult selectMessageCount(){
        try {
            LoginUser loginUser = Optional.ofNullable(SecurityUtils.getLoginUser()).orElseThrow(() -> new BaseException("请先登录"));
            return homepageService.selectMessageCount(loginUser);
        } catch (BaseException e) {
            LOGGER.error("获取各个模块的待办数据失败",e);
            return AjaxResult.error();
        }
    }


    /**
     * 首页-简单看板-订单数据统计
     * @return
     */
    @PostMapping("/simple/kanban/Info")
    public AjaxResult selectSimpleKanbanInfo(@RequestBody SimpleKanbanInConditionVO simpleKanbanInConditionVO){
        try {
            return homepageService.selectSimpleKanbanInfo(simpleKanbanInConditionVO);
        } catch (BaseException e) {
            LOGGER.error("获取首页-简单看板-订单数据统计失败",e);
            return AjaxResult.error();
        }
    }

}
